package com.guigu.www.denghao.controller;

import com.guigu.www.denghao.service.DHBespeakResultService;
import com.guigu.www.pojo.Bespeak;
import com.guigu.www.pojo.BespeakResult;
import com.guigu.www.pojo.PageVo;
import com.guigu.www.pojo.PersonClassification;
import com.guigu.www.utils.FtpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/DHBespeakResultController")
public class DHBespeakResultController {

    @Autowired
    private DHBespeakResultService resultService;

    //分页查询预约列表
    @PostMapping("/selbespeakall")
    public PageVo<Bespeak> selall(@RequestBody Map<String,String> map){
        PageVo<Bespeak> pageVo = resultService.selldespeakall(map);
        return pageVo;
    }

    @PostMapping("/queryClassificationAll")
    public List<PersonClassification> queryClassificationAll(){
        return resultService.queryClassificationAll();
    }

    @PostMapping("/submitUpload")
    public Boolean submitUpload(@RequestParam("file1") MultipartFile file1, BespeakResult bespeakResult,HttpServletRequest request) throws IOException {
        if(file1.isEmpty()){
            return false;
        }else{
            // 获取上传的文件流
            InputStream  inputStream = file1.getInputStream();
            //String realPath = "";
            //获取文件原名
            String originalFilename = file1.getOriginalFilename();
            //获取源文件前缀
            String fileNamePrefix = originalFilename.substring(0,originalFilename.lastIndexOf("."));
            //获取源文件后缀
            String fileNameSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            // 将源文件前缀之后加上时间戳避免重名
            String newFileNamePrefix = fileNamePrefix + new Date().getTime();
            //得到上传后新文件的文件名
            String newFileName = newFileNamePrefix+fileNameSuffix;
            //创建一个新的File对象用于存放上传的文件
            /*File fileNew = new File(realPath + newFileName);
            try {
                file1.transferTo(fileNew);
            } catch (IOException e) {
                e.printStackTrace();
            }*/
            boolean b = FtpUtil.uploadFile(newFileName, inputStream);
            //System.out.println(b);
            bespeakResult.setBesResultZip(newFileName);
            resultService.submitUpload(bespeakResult);
        }
        return true;
    }

    @PostMapping("/showUpload")
    public BespeakResult showUpload(Integer besId){
        return resultService.showUpload(besId);
    }
}
